﻿// Copyright (c) 2014 Bryan C. O'Connell
// http://wsubi.codeplex.com/license
// Purpose: Guard protects the application. This class holds methods used to perform
// various safety checks for common reference by all classes in the system.

// TODO: Add a 'CallingMethod' string to each method so it's easier to determine where an error might be coming from.

using System;
using System.IO;
using System.Collections.Generic;

namespace Wsubi.Code.Utilities
{
    static class Guard
    {
        public static void ConfirmFolderExists(string FolderToFind)
        {
            if (!Directory.Exists(FolderToFind)) {
                throw new DirectoryNotFoundException("Guard: The directory you specified does not exist.");
            }
        }

        public static void ConfirmFileExists(string FileToFind)
        {
            if (!File.Exists(FileToFind)) {
                throw new FileNotFoundException("Guard: The file you specified does not exist.", FileToFind);
            }
        }

        public static void CheckForEmptyString(string StringToCheck)
        { 
            if (String.IsNullOrEmpty(StringToCheck) || String.IsNullOrWhiteSpace(StringToCheck)) {
                throw new FormatException("Guard: String argument cannot be null, empty or consist only of whitespace.");
            }
        }

        public static void MinimumNumberOfArrayElements(string[] ArrayToCheck, int MinimumNumberOfElements)
        { 
            if ( (ArrayToCheck == null) || (ArrayToCheck.Length < MinimumNumberOfElements)) {
                throw new IndexOutOfRangeException("Guard: The specified array contains too few elements.");
            }
        }

        public static void CheckForNullObject(object Object)
        { 
            if (Object == null) {
                throw new NullReferenceException("Guard: The specified object is null, which is not valid in this situation.");
            }
        }

        public static void CheckForKey(Dictionary<string, string> DictionaryToCheck, string KeyToFind)
        { 
            if (!DictionaryToCheck.ContainsKey(KeyToFind)) {
                throw new KeyNotFoundException("Guard: The specified dictionaty does not contain the given key.");
            }
        }
    }
}
